package main

import "fmt"

type SeatManager struct {
	seatMap map[int]int
	next    int
	n       int
}

func Constructor(n int) SeatManager {
	seat := SeatManager{}
	seat.seatMap = make(map[int]int, n)
	for i := 1; i < n+1; i++ {
		//初始默认设置为0，未预约
		seat.seatMap[i] = 0
	}
	seat.n = n
	seat.next = 1
	return seat
}

// 返回可预约的编号的最小值，没有则返回nil
func (this *SeatManager) Reserve() int {
	//避免查询整个seat
	if this.next <= this.n && this.seatMap[this.next] == 0 {
		return this.next
	}
	//确定下标位置
	index := len(this.seatMap) + 1
	for k, v := range this.seatMap {
		if v == 0 {
			index = min(index, k)
		}
	}
	if index == len(this.seatMap)+1 {
		return 0
	}
	//更改对应位置坐标为1 已预约
	this.seatMap[index] = 1
	//设置下一个预约的位置
	this.next = index + 1
	return index
}

// 已预约的取消预约
func (this *SeatManager) Unreserve(seatNumber int) {
	this.seatMap[seatNumber] = 0
}

func main() {
	seat := Constructor(5)
	fmt.Println(seat.Reserve())
	seat.Unreserve(1)
	fmt.Println(seat.seatMap)
}

/**
 * Your SeatManager object will be instantiated and called as such:
 * obj := Constructor(n);
 * param_1 := obj.Reserve();
 * obj.Unreserve(seatNumber);
 */
